
#include <BPainter>
#include <BLabel>
#include <BFileDialog>
#include <BGridLayout>
#include <BHBoxLayout>
#include <BApplication>

#include "Config.h"
#include "PathSettingDialog.h"

PathSettingDialog::PathSettingDialog(BWidget* parent) : BDialog(parent)
{
	this->setTitle("Path Setting");

	BGridLayout* gridLayout = new BGridLayout(this);
	{
		_headerPathEdit = new BLineEdit();
		_headerBrowseButton = new BButton("...");
		_manualPathEdit = new BLineEdit();
		_manualBrowseButton = new BButton("...");

		gridLayout->addWidget(0, 0, new BLabel("header path : "));
		gridLayout->addWidget(0, 1, _headerPathEdit);
		gridLayout->addWidget(0, 2, _headerBrowseButton);
		gridLayout->addWidget(1, 0, new BLabel("manual path : "));
		gridLayout->addWidget(1, 1, _manualPathEdit);
		gridLayout->addWidget(1, 2, _manualBrowseButton);
		gridLayout->setColumnStretch(2, 0);
	}

	BHBoxLayout* bottomLayout = new BHBoxLayout("bottom", this, Part_Bottom);
	{
		_resetButton = new BButton("Reset");
		_confirmButton = new BButton("Confirm");
		_cancelButton = new BButton("Cancel");
		bottomLayout->addWidget(_resetButton);
		bottomLayout->addSpring();
		bottomLayout->addWidget(_confirmButton);
		bottomLayout->addWidget(_cancelButton);
	}

	connect(_headerBrowseButton, Signal_Clicked, &PathSettingDialog::slotHeaderBrowseButtonClicked);
	connect(_manualBrowseButton, Signal_Clicked, &PathSettingDialog::slotManualBrowseButtonClicked);

	connect(_resetButton, Signal_Clicked, &PathSettingDialog::slotResetButtonClicked);
	connect(_confirmButton, Signal_Clicked, &PathSettingDialog::slotConfirmButtonClicked);
	connect(_cancelButton, Signal_Clicked, &PathSettingDialog::slotCancelButtonClicked);

	if (Config* config = BApplication::property("config"))
	{
		_headerPathEdit->setText(config->headerPath());
		_manualPathEdit->setText(config->manualPath());
	}
}
PathSettingDialog::~PathSettingDialog()
{

}

void PathSettingDialog::setHeaderPath(const BString& headerPath)
{
	if (Config* config = BApplication::property("config"))
	{
		config->setHeaderPath(headerPath);
		_headerPathEdit->setText(headerPath);
	}
}
const BString& PathSettingDialog::headerPath() const
{
	return _headerPathEdit->text();
}

void PathSettingDialog::setManualPath(const BString& manualPath)
{
	if (Config* config = BApplication::property("config"))
	{
		config->setManualPath(manualPath);
		_manualPathEdit->setText(manualPath);
	}
}
const BString& PathSettingDialog::manualPath() const
{
	return _manualPathEdit->text();
}

void PathSettingDialog::slotResetButtonClicked()
{

}
void PathSettingDialog::slotConfirmButtonClicked()
{
	this->close(true);
}
void PathSettingDialog::slotCancelButtonClicked()
{
	this->close();
}

void PathSettingDialog::slotHeaderBrowseButtonClicked()
{
	const BString& fileName =BFileDialog::OpenFolder(this, "Choose header path");
	if(fileName.size())
	{
		_headerPathEdit->setText(fileName);
		if (Config* config = BApplication::property("config"))
		{
			config->setHeaderPath(fileName);
		}
	}
}
void PathSettingDialog::slotManualBrowseButtonClicked()
{
	const BString& fileName = BFileDialog::OpenFolder(this, "Choose header path");
	if (fileName.size())
	{
		_manualPathEdit->setText(fileName);
		if (Config* config = BApplication::property("config"))
		{
			config->setManualPath(fileName);
		}
	}
}
